﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;

public partial class my_ecrm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
        }
        else
        {
            TUser1.CheckUser(this);
            
            btnNew.Attributes.Add("OnClick", "javascript:return chk();");
            btnEdit.Attributes.Add("OnClick", "javascript:return chk();");
            ne_unittype.Attributes.Add("OnClick", "javascript:return togglesrv();");

            //设定状态
            if ((this.Session["ecrm_email"] == null) || (this.Session["ecrm_email"].ToString() == ""))
            {
                userstatus1.Text = "你好，请 <a href=\"main.aspx\">登录</a>或 ";
                userstatus2.Text = "<a href=\"./user/user_reg.aspx\">注册</a>";
            }
            else
            {
                string sid = this.Session["ecrm_uid"].ToString();
                TUser oUser = new TUser(sid);
                oUser.Select();
                string bigname = oUser.BigName;

                userstatus1.Text = "<a onclick='javascript:location.href=\"./user/user_myself.aspx\"' onmouseover='this.style.cursor=\"pointer\"' onmouseout='this.style.backgroundColor=\"\"'>" + bigname + "的设置</a>";
                userstatus2.Text = "<a onclick='javascript:location.href=\"./user/user_logout.aspx\"' onmouseover='this.style.cursor=\"pointer\"' onmouseout='this.style.backgroundColor=\"\"'>退出</a>";
                
                TCommon.LoadComBox("A01", ne_unittype);

                //gotomyplan.Text = "<a href=\"my_plan.aspx?myuid=" + sid + "\" target=_blank>进入我的周计划</a>";

                //linkmanlist.Text = ListMans(sid);
            }

        };
    }

    private string ListMans(string uid)
    {
        string result = "<table id=\"alllinkman\" cellSpacing=\"1\" cellPadding=\"0\" width=\"98%\" align=\"center\" border=\"0\" class=\"tablesorter\">";
        result += "<thead>";
        result += "<tr height=26>";
        result += "<th width=40 align=left>序号</th>";
        result += "<th width=70 align=left>姓名</th>";
        result += "<th width=40 align=left>性别</th>";
        result += "<th align=center>公司</th>";
        result += "</tr>";
        result += "</thead>";

        int iRow = 1;
        result += "<tbody>";
        string sSql = "select uid,linkman,sex,companyabb from t_linkman where inman='"+uid+"' order by iid desc";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        while (myReader.FDataReader.Read())
        {
            result += "<tr height=26>";
            result += "<td width=40 align=center>" + iRow.ToString() + "</td>";
            result += "<td width=70><a href=\"javascript:;\" onclick=\"getlinkman('" + myReader.FDataReader.GetValue(0).ToString() + "')\">" + myReader.FDataReader.GetValue(1).ToString() + "</a></td>";
            result += "<td width=40 align=center>" + myReader.FDataReader.GetValue(2).ToString() + "</td>";
            result += "<td>" + myReader.FDataReader.GetValue(3).ToString() + "</td>";
            result += "</tr>";
            iRow++;
        }
        myReader.Free();
        result += "<tbody>";

        result += "</table>";

        //lt_have.Text = CountMans(uid);

        return result;
    }

    private string CountMans(string uid)
    {
        string result = "";

        string sSql = "select infoid,infoname from t_sys_info2 where isshow='Y' and typeid='A01' order by infoid";
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        while (myReader.FDataReader.Read())
        {
            string infoid = myReader.FDataReader.GetValue(0).ToString();
            string infoname = myReader.FDataReader.GetValue(1).ToString();

            result += "<a href=\"my_ecrm2.aspx?unitcode="+infoid+"\">"+infoname + "("+myCountMan(uid,infoid).ToString()+"人)</a>&nbsp;&nbsp;";
        }
        myReader.Free();
        
        return result;
    }

    private int myCountMan(string uid, string infoid)
    {
        int result = 0;

        string sSql = "select count(*) from t_linkman where status=0 and inman='" + uid + "' and unittype='" + infoid + "'";
        TSqlDBReader2 oRS = new TSqlDBReader2(sSql);
        if (oRS.FDataReader.Read())
        {
            result = (int)oRS.FDataReader.GetValue(0);
        }
        oRS.Free();

        return result;
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {
        string sid = this.Session["ecrm_uid"].ToString();
        
        TLinkman oMan = new TLinkman();
        //必填---
        oMan.Linkman = ne_linkman.Text;
        oMan.ImpLevel = ne_implevel.SelectedValue;
        oMan.SrvCompany = ne_srvcompany.Text;
        if(ne_male.Checked)
        {
            oMan.Sex = "先生";
        }
        if(ne_female.Checked)
        {
            oMan.Sex = "女士";
        }
        oMan.CompanyAbb = ne_companyabb.Text;
        oMan.UnitType = ne_unittype.SelectedValue;
        oMan.Dept = ne_dept.Text;
        oMan.JobPosi = ne_jobposi.Text;
        oMan.Mobile = ne_mobile.Text;
        oMan.Tel = ne_tel.Text;
        oMan.EMail = ne_email.Text;
        //选填---
        oMan.Age = ne_age.Text;
        oMan.Birthday = ne_birthday.Text;
        oMan.Company = ne_company.Text;
        oMan.CompanyAddr = ne_companyaddr.Text;
        oMan.CompanyPostcode = ne_companypostcode.Text;
        oMan.Fax = ne_fax.Text;
        oMan.Favr = ne_favr.Text;
        oMan.Notice = ne_notice.Text;
        //---
        oMan.InMan = sid;
        //---
        oMan.Insert();
        //---
        //linkmanlist.Text = ListMans(sid);
    }
    protected void dlRS_ItemCommand(object source, DataListCommandEventArgs e)
    {
        LinkButton b = (LinkButton)e.CommandSource;
        switch (b.CommandName)
        {
            case "modify":
                string suid = b.CommandArgument;
                uid.Text = suid;
                TLinkman oMan = new TLinkman(suid);
                oMan.Select();
                //必填---
                ne_linkman.Text = oMan.Linkman;
                TCommon.SetCombox(oMan.ImpLevel, ne_implevel);
                ne_srvcompany.Text = oMan.SrvCompany;
                if (oMan.Sex == "先生")
                {
                    ne_male.Checked = true;
                    ne_female.Checked = false;
                }
                else
                {
                    ne_male.Checked = false;
                    ne_female.Checked = true;
                }
                ne_companyabb.Text = oMan.CompanyAbb;
                TCommon.SetCombox(oMan.UnitType, ne_unittype);
                ne_dept.Text = oMan.Dept;
                ne_jobposi.Text = oMan.JobPosi;
                ne_mobile.Text = oMan.Mobile;
                ne_tel.Text = oMan.Tel;
                ne_email.Text = oMan.EMail;
                //选填---
                ne_age.Text = oMan.Age;
                ne_birthday.Text = oMan.Birthday;
                ne_company.Text = oMan.Company;
                ne_companyaddr.Text = oMan.CompanyAddr;
                ne_companypostcode.Text = oMan.CompanyPostcode;
                ne_fax.Text = oMan.Fax;
                ne_favr.Text = oMan.Favr;
                ne_notice.Text = oMan.Notice;
                //---
                break;
            case "remove":
                TLinkman.DeleteByID(b.CommandArgument);
                //linkmanlist.Text = ListMans(this.Session["ecrm_uid"].ToString());
                break;
        }
    }
    protected void btnEdit_Click(object sender, EventArgs e)
    {
        TLinkman oMan = new TLinkman(uid.Text);
        oMan.Linkman = ne_linkman.Text;
        oMan.ImpLevel = ne_implevel.SelectedValue;
        oMan.SrvCompany = ne_srvcompany.Text;
        if (ne_male.Checked)
        {
            oMan.Sex = "先生";
        }
        if (ne_female.Checked)
        {
            oMan.Sex = "女士";
        }
        oMan.CompanyAbb = ne_companyabb.Text;
        oMan.UnitType = ne_unittype.SelectedValue;
        oMan.Dept = ne_dept.Text;
        oMan.JobPosi = ne_jobposi.Text;
        oMan.Mobile = ne_mobile.Text;
        oMan.Tel = ne_tel.Text;
        oMan.EMail = ne_email.Text;
        //选填---
        oMan.Age = ne_age.Text;
        oMan.Birthday = ne_birthday.Text;
        oMan.Company = ne_company.Text;
        oMan.CompanyAddr = ne_companyaddr.Text;
        oMan.CompanyPostcode = ne_companypostcode.Text;
        oMan.Fax = ne_fax.Text;
        oMan.Favr = ne_favr.Text;
        oMan.Notice = ne_notice.Text;
        //---
        oMan.Update();
        //---
        //linkmanlist.Text = ListMans(this.Session["ecrm_uid"].ToString());
    }
    /*
    protected void btnFind_Click(object sender, EventArgs e)
    {
        this.Response.Redirect("find_linkman.aspx?fcode=b&svalue=" + txtName.Text.Trim() + "&ucode=");
    }
    */
}
